# -*- coding:utf-8 -*-
from users.models import BusinessPermission, CompanyInfo, CompanyPerson
import json


def has_permission(path, permission, method, role, post_data, get_data):

    pmap = {
        # 工单
        "task_get": BusinessPermission.PERMISSION_TASK_GET,
        "task_put": BusinessPermission.PERMISSION_TASK_PUT,
        "task_delete": BusinessPermission.PERMISSION_TASK_DELETE,
        "task_post": BusinessPermission.PERMISSION_TASK_POST,

        # 合同
        "contract_get": BusinessPermission.PERMISSION_CONTRACT_GET,
        "contract_put": BusinessPermission.PERMISSION_CONTRACT_PUT,
        "contract_delete": BusinessPermission.PERMISSION_CONTRACT_DELETE,
        "contract_post": BusinessPermission.PERMISSION_CONTRACT_POST,

        # 电梯管理
        "elevator_get": BusinessPermission.PERMISSION_ELEVATOR_GET,
        "elevator_put": BusinessPermission.PERMISSION_ELEVATOR_PUT,
        "elevator_delete": BusinessPermission.PERMISSION_ELEVATOR_DELETE,
        "elevator_post": BusinessPermission.PERMISSION_ELEVATOR_POST,

        # 电梯绑定
        "elevator_bind_get": BusinessPermission.PERMISSION_ELEVATOR_BIND_GET,
        "elevator_bind_put": BusinessPermission.PERMISSION_ELEVATOR_BIND_PUT,
        # "elevator_bind_post": BusinessPermission.PERMISSION_ELEVATOR_BIND_DELETE,
        # BusinessPermission.PERMISSION_ELEVATOR_BIND_POST = "43"

        # 维保历史
        "history_get": BusinessPermission.PERMISSION_MAINTAIN_HISTORY_GET,
        "history_put": BusinessPermission.PERMISSION_MAINTAIN_HISTORY_PUT,
        "history_delete": BusinessPermission.PERMISSION_MAINTAIN_HISTORY_DELETE,
        "history_post": BusinessPermission.PERMISSION_MAINTAIN_HISTORY_POST,

        # 维保单位
        "maintain_company_get": BusinessPermission.PERMISSION_MAINTAIN_COMPANY_GET,
        "maintain_company_put": BusinessPermission.PERMISSION_MAINTAIN_COMPANY_PUT,
        "maintain_company_delete": BusinessPermission.PERMISSION_MAINTAIN_COMPANY_DELETE,
        "maintain_company_post": BusinessPermission.PERMISSION_MAINTAIN_COMPANY_POST,

        # 物业单位
        "property_company_get": BusinessPermission.PERMISSION_PROPERTY_COMPANY_GET,
        "property_company_put": BusinessPermission.PERMISSION_PROPERTY_COMPANY_PUT,
        "property_company_delete": BusinessPermission.PERMISSION_PROPERTY_COMPANY_DELETE,
        "property_company_post": BusinessPermission.PERMISSION_PROPERTY_COMPANY_POST,

        # 维保人员
        "person_get": BusinessPermission.PERMISSION_MAINTAIN_PERSON_GET,
        "person_put": BusinessPermission.PERMISSION_MAINTAIN_PERSON_PUT,
        "person_delete": BusinessPermission.PERMISSION_MAINTAIN_PERSON_DELETE,
        "person_post": BusinessPermission.PERMISSION_MAINTAIN_PERSON_POST,

        # 维修类型
        "repair_type_get": BusinessPermission.PERMISSION_REPAIR_TYPE_GET,
        "repair_type_put": BusinessPermission.PERMISSION_REPAIR_TYPE_PUT,
        "repair_type_delete": BusinessPermission.PERMISSION_REPAIR_TYPE_DELETE,
        "repair_type_post": BusinessPermission.PERMISSION_REPAIR_TYPE_POST,

        # 维修
        "repair_get": BusinessPermission.PERMISSION_REPAIR_GET,
        "repair_put": BusinessPermission.PERMISSION_REPAIR_PUT,
        "repair_delete": BusinessPermission.PERMISSION_REPAIR_DELETE,
        "repair_post": BusinessPermission.PERMISSION_REPAIR_POST,

        # 维修项目
        "repair_content_get": BusinessPermission.PERMISSION_REPAIR_CONTENT_GET,
        "repair_content_put": BusinessPermission.PERMISSION_REPAIR_CONTENT_PUT,
        "repair_content_delete": BusinessPermission.PERMISSION_REPAIR_CONTENT_DELETE,
        "repair_content_post": BusinessPermission.PERMISSION_REPAIR_CONTENT_POST,

        # 外设
        "peripheral_get": BusinessPermission.PERMISSION_PERIPHERAL_GET,
        "peripheral_put": BusinessPermission.PERMISSION_PERIPHERAL_PUT,
        "peripheral_delete": BusinessPermission.PERMISSION_PERIPHERAL_DELETE,
        "peripheral_post": BusinessPermission.PERMISSION_PERIPHERAL_POST,

        # 维保单位账户
        "maintain_account_get": BusinessPermission.PERMISSION_MAINTAIN_COMPANY_ACCOUNT_GET,
        "maintain_account_put": BusinessPermission.PERMISSION_MAINTAIN_COMPANY_ACCOUNT_PUT,
        "maintain_account_delete": BusinessPermission.PERMISSION_MAINTAIN_COMPANY_ACCOUNT_DELETE,
        "maintain_account_post": BusinessPermission.PERMISSION_MAINTAIN_COMPANY_ACCOUNT_POST,

        # 使用单位账户
        "property_account_get": BusinessPermission.PERMISSION_PROPERTY_COMPANY_ACCOUNT_GET,
        "property_account_put": BusinessPermission.PERMISSION_PROPERTY_COMPANY_ACCOUNT_PUT,
        "property_account_delete": BusinessPermission.PERMISSION_PROPERTY_COMPANY_ACCOUNT_DELETE,
        "property_account_post": BusinessPermission.PERMISSION_PROPERTY_COMPANY_ACCOUNT_POST,

        # 日志
        "log_get": BusinessPermission.PERMISSION_LOG_GET,
        "log_put": BusinessPermission.PERMISSION_LOG_PUT,
        "log_delete": BusinessPermission.PERMISSION_LOG_DELETE,
        "log_post": BusinessPermission.PERMISSION_LOG_POST,

        # 单梯监控
        "single_monitor_get": BusinessPermission.PERMISSION_SINGLE_MONITORS_GET,
        "single_monitor_put": BusinessPermission.PERMISSION_SINGLE_MONITORS_PUT,
        "single_monitor_delete": BusinessPermission.PERMISSION_SINGLE_MONITORS_DELETE,
        "single_monitor_post": BusinessPermission.PERMISSION_SINGLE_MONITORS_POST,

        # 多梯监控
        "multiple_monitor_get": BusinessPermission.PERMISSION_MULTIPLE_MONITORS_GET,
        "multiple_monitor_put": BusinessPermission.PERMISSION_MULTIPLE_MONITORS_PUT,
        "multiple_monitor_delete": BusinessPermission.PERMISSION_MULTIPLE_MONITORS_DELETE,
        "multiple_monitor_post": BusinessPermission.PERMISSION_MULTIPLE_MONITORS_POST,

        # 电梯故障记录
        "fault_record_get": BusinessPermission.PERMISSION_FAULT_RECORD_GET,
        # 电梯故障数
        "fault_number_get": BusinessPermission.PERMISSION_FAULT_NUMBERS_STATISTICS_GET,
        # 电梯使用地点故障数
        "fault_position_get": BusinessPermission.PERMISSION_FAULT_POSITION_STATISTICS_GET,

        # 电梯统计
        "elevator_statistics": BusinessPermission.PERMISSION_ELEVATOR_STATISTICS,
        # 电梯分类统计
        "elevator_classification_statistics": BusinessPermission.PERMISSION_ELEVATOR_CLASSIFICATION_STATISTICS,
        # 电梯概况统计
        "elevator_situation_statistics": BusinessPermission.PERMISSION_ELEVATOR_SITUATION_STATISTICS,
        # 电梯使用单位统计
        "elevator_property_statistics": BusinessPermission.PERMISSION_ELEVATOR_PROPERTY_COMPANY_STATISTICS,
        # 电梯维保单位统计
        "elevator_maintain_statistics": BusinessPermission.PERMISSION_ELEVATOR_MAINTAIN_COMPANY_STATISTICS,
        # 电梯品牌统计
        "elevator_brand_statistics": BusinessPermission.PERMISSION_ELEVATOR_BRAND_STATISTICS,
        # 电梯维保状态统计
        "elevator_status_statistics": BusinessPermission.PERMISSION_ELEVATOR_MAINTAIN_STATUS_STATISTICS,
        # 电梯运行状态统计
        "elevator_run_statistics": BusinessPermission.PERMISSION_ELEVATOR_RUN_STATUS_STATISTICS,
        # 保
        # 电梯维修状态统计
        "elevator_repair_statistics": BusinessPermission.PERMISSION_ELEVATOR_REPAIR_STATUS_STATISTICS,
        # 电梯救援状态统计
        "elevator_rescue_statistics": BusinessPermission.PERMISSION_ELEVATOR_RESCUE_STATUS_STATISTICS,
        # 电梯体检分析
        "elevator_checkup": BusinessPermission.PERMISSION_CHECKUP_STATISTICS,

        # 管理角色
        "role_get": BusinessPermission.PERMISSION_ADMIN_ROLE_GET,
        "role_put": BusinessPermission.PERMISSION_ADMIN_ROLE_PUT,
        "role_delete": BusinessPermission.PERMISSION_ADMIN_ROLE_DELETE,
        "role_post": BusinessPermission.PERMISSION_ADMIN_ROLE_POST,

        # 角色 关联权限
        # "permission_role_get": BusinessPermission.PERMISSION_ROLE_GET,
        # "permission_role_put": BusinessPermission.PERMISSION_ROLE_PUT,
        # "permission_role_delete": BusinessPermission.PERMISSION_ROLE_DELETE,
        # "permission_role_post": BusinessPermission.PERMISSION_ROLE_POST,

        # 账户
        "user_get": BusinessPermission.PERMISSION_USER_GET,
        "user_put": BusinessPermission.PERMISSION_USER_PUT,
        "user_delete": BusinessPermission.PERMISSION_USER_DELETE,
        "user_post": BusinessPermission.PERMISSION_USER_POST,
        "organization_get": BusinessPermission.PERMISSION_ORGANIZATION_GET,
        "organization_put": BusinessPermission.PERMISSION_ORGANIZATION_PUT,
        "organization_delete": BusinessPermission.PERMISSION_ORGANIZATION_DELETE,
        "organization_post": BusinessPermission.PERMISSION_ORGANIZATION_POST,
        # 首页
        # "month": BusinessPermission.PERMISSION_MONTH_GET,
        # "third": BusinessPermission.PERMISSION_THIRD_GET,
        # "count": BusinessPermission.PERMISSION_COUNT_GET
        "index_get": BusinessPermission.PERMISSION_INDEX_GET,

        # 平台接入
        "platform_get": BusinessPermission.PERMISSION_PLATFORM_GET,
        "platform_put": BusinessPermission.PERMISSION_PLATFORM_PUT,
        "platform_delete": BusinessPermission.PERMISSION_PLATFORM_DELETE,
        "platform_post": BusinessPermission.PERMISSION_PLATFORM_POST,

        # 制造单位
        "manu_get": BusinessPermission.PERMISSION_MANUFACTURER_GET,
        "manu_put": BusinessPermission.PERMISSION_MANUFACTURER_PUT,
        "manu_delete": BusinessPermission.PERMISSION_MANUFACTURER_DELETE,
        "manu_post": BusinessPermission.PERMISSION_MANUFACTURER_POST,

        "summary_get": BusinessPermission.PERMISSION_SUMMARY_GET,
        "fault_get": BusinessPermission.PERMISSION_FAULT_STATISTICS_ANALYSIS,

        # 配件管理
        "fittings_get": BusinessPermission.PERMISSION_FITTINGS_GET,
        "fittings_put": BusinessPermission.PERMISSION_FITTINGS_PUT,
        "fittings_delete": BusinessPermission.PERMISSION_FITTINGS_DELETE,
        "fittings_post": BusinessPermission.PERMISSION_FITTINGS_POST,

        # 故障会诊及分析
        "fault_analysis_get": BusinessPermission.PERMISSION_ELEVATOR_FAULT_ANALYSIS_GET,
        "fault_analysis_put": BusinessPermission.PERMISSION_ELEVATOR_FAULT_ANALYSIS_PUT,
        "fault_analysis_delete": BusinessPermission.PERMISSION_ELEVATOR_FAULT_ANALYSIS_DELETE,
        "fault_analysis_post": BusinessPermission.PERMISSION_ELEVATOR_FAULT_ANALYSIS_POST,
    }
    # 工单
    if path.startswith("/api/v1/maintain/task/"):
        return pmap.get("task_" + method) in permission

    # 合同
    if path.startswith("/api/v1/maintain/contract/"):
        return pmap.get("contract_" + method) in permission
    # 电梯
    if path.startswith("/api/v1/basic/elevator/"):
        return pmap.get("elevator_" + method) in permission

    # 电梯人员绑定get
    if path.startswith("/api/v1/basic/bind/") and method == "get":
        return pmap.get("elevator_bind_get") in permission

    # 电梯人员绑定update
    if path.startswith("/api/v1/basic/detail/"):
        return pmap.get("elevator_bind_" + method) in permission

    if path.startswith("/api/v1/user/person/bind/") and method == "get":
        return pmap.get("elevator_bind_" + "put") in permission

    # 维保历史
    if path.startswith("/api/v1/maintain/history/"):
        return pmap.get("history_" + method) in permission

    # 管理员创建维保单位
    # if path.startswith("/api/v1/user/company/"):
    #     if method == "post":
    #         post_data = json.loads(post_data)
    #         company_type = post_data.get("company_type", None)
    #         if not company_type:
    #             return False
    #         elif int(company_type) == 100:
    #             return pmap.get("maintain_company_post") in permission
    #         elif int(company_type) == 300:
    #             return pmap.get("property_company_post") in permission
    #         return False
    #     elif method == "get":
    #         company_type = get_data.get("company_type", None)
    #         if not company_type:
    #             if path.split("/") == 7:
    #                 pk = path.split("/")[-2]
    #                 com = CompanyInfo.objects.get(id=int(pk))
    #                 if com.company_type == 100:
    #                     return pmap.get("maintain_company_get") in permission
    #                 elif com.company_type == 300:
    #                     return pmap.get("property_company_get") in permission
    #                 else:
    #                     return False
    #             return pmap.get("maintain_company_get") in permission
    #         elif int(company_type) == 100:
    #             if get_data.get("statistics", None) == "1":
    #                 return pmap.get("index_get") in permission
    #             return pmap.get("maintain_company_get") in permission
    #         elif int(company_type) == 300:
    #             if get_data.get("statistics", None) == "1":
    #                 return pmap.get("index_get") in permission
    #             return pmap.get("property_company_get") in permission
    #         return False
    #     elif method == "put":
    #         post_data = json.loads(post_data)
    #         company_type = post_data.get("company_type", None)
    #         if not company_type:
    #             return False
    #         elif int(company_type) == 100:
    #             return pmap.get("maintain_company_put") in permission
    #         elif int(company_type) == 300:
    #             return pmap.get("property_company_put") in permission
    #         return False
    #     elif method == "delete":
    #         cid = path.split("/")[5]
    #         c = CompanyInfo.objects.filter_active(id=int(cid)).first()
    #         if not c:
    #             return False
    #         elif c.company_type == 300:
    #             return pmap.get("property_company_delete") in permission
    #         elif c.company_type == 100:
    #             return pmap.get("maintain_company_delete") in permission
    #     return False
    if path.startswith("/api/v1/user/company/"):
        return pmap.get("maintain_company_" + method) in permission

    if path.startswith("/api/v1/user/organization/organize/"):
        return pmap.get("maintain_company_" + method) in permission or pmap.get("role_" + method) in permission

    if path.startswith("/api/v1/user/organization/code/"):
        return pmap.get("maintain_company_" + method) in permission

    if path.startswith("/api/v1/user/organization/mycode/"):
        return pmap.get("maintain_company_" + method) in permission

    # 维保人员
    if path.startswith("/api/v1/user/person/"):
        return pmap.get("person_" + method) in permission

    # 维修项目
    if path.startswith("/api/v1/manu/elevator/content/"):
        return pmap.get("repair_content_" + method) in permission

    # 外设
    if path.startswith("/api/v1/manu/collection/"):
        return pmap.get("peripheral_" + method) in permission

    # 维修
    if path.startswith("/api/v1/manu/elevator/repair/"):
        return pmap.get("repair_" + method) in permission

    # 维修类型
    if path.startswith("/api/v1/manu/elevator/type/"):
        return pmap.get("repair_content_" + method) in permission

    # 创建单位账户
    if path.startswith("/api/v1/user/bind/"):
        if method == "post":
            person_type = post_data.get("person_type", None)
            if not person_type:
                return False
            elif person_type == 10:
                return pmap.get("maintain_account_post") in permission
            elif person_type == 20:
                return pmap.get("property_account_post") in permission
        elif method == "put":
            person_type = post_data.get("person_type", None)
            if not person_type:
                return False
            elif person_type == 10:
                return pmap.get("maintain_account_put") in permission
            elif person_type == 20:
                return pmap.get("property_account_put") in permission
        elif method == "get":
            person_type = get_data.get("person_type", None)
            if not person_type:
                return False
            elif person_type == 10:
                return pmap.get("maintain_account_get") in permission
            elif person_type == 20:
                return pmap.get("property_account_get") in permission
        elif method == "delete":
            cid = path.split("/")[5]
            cp = CompanyPerson.objects.filter_active(id=int(cid)).first()
            if not cp:
                return False
            if cp.person_type == 10:
                return pmap.get("maintain_account_delete") in permission
            else:
                return pmap.get("property_account_delete") in permission

    # 日志
    if path.startswith("/api/v1/user/logs/"):
        return pmap.get("log_" + method) in permission

    # 多梯监控状态
    if path.startswith("/api/v1/basic/monitor/multi/"):
        return pmap.get("multiple_monitor_get") in permission

    # 监控 分 单梯和多梯
    if path.startswith("/api/v1/manu/elevator/"):
        return pmap.get("single_monitor_get") in permission or pmap.get("multiple_monitor_get") in permission

    # 单梯运行状态
    if path.startswith("/api/v1/basic/sys/") and method == "get":
        return pmap.get("single_monitor_get") in permission

    # 单梯故障信息
    if path.startswith("/api/v1/manu/elevator/alarmsV2/"):
        return pmap.get("single_monitor_get") in permission

    # # 多梯运行状态
    # if path.startswith("/api/v1/manu/elevator/status/") and method == "get":
    #     return pmap.get("multiple_monitor_get") in permission

    # 电梯故障记录
    if path == "/api/v1/manu/elevator/alarms/":
        return pmap.get("fault_record_get") in permission

    # 电梯故障数
    if path == "/api/v1/manu/elevator/alarms/statistics/":
        return pmap.get("fault_number_get") in permission

    # 电梯使用地点故障数
    # TODO 接口未接
    # if path.startswith("/api/v1/maintain/task/"):
    #     return pmap.get("fault_position_get") in permission
    if path.startswith("/api/v1/user/user/"):
        return pmap.get("user_" + method) in permission

    # 角色
    if path.startswith("/api/v1/user/role/"):
        return pmap.get("role_" + method) in permission

    # 角色页面 权限
    if path.startswith("/api/v1/user/permissions/"):
        return pmap.get("role_get") in permission or pmap.get("role_put") in permission or pmap.get("role_post") in permission

    # 首页统计
    if path.startswith("/api/v1/basic/statistics/"):
        # return pmap.get("month") in permission
        return pmap.get("index_get") in permission

    # 首页 第三方
    if path.startswith("/api/v1/basic/third/"):
        # return pmap.get("third") in permission
        return pmap.get("index_get") in permission

    # 首页人员统计
    if path.startswith("/api/v1/user/count/"):
        # return pmap.get("count") in permission
        return pmap.get("index_get") in permission

    # 企业平台
    if path.startswith("/api/v1/manu/platform/"):
        return pmap.get("platform_" + method) in permission

    # 制造厂商
    if path.startswith("/api/v1/manu/manufacturer/"):
        return pmap.get("manu_" + method) in permission

    # 工单导出
    if path.startswith("/api/v1/user/download/"):
        return pmap.get("task_get") in permission

    # 平台概况
    if path.startswith("/api/v1/sta/statistics/summary/"):
        return pmap.get("summary_get") in permission

    # 电梯统计分析
    if path.startswith("/api/v1/manu/elevator/alarm/organization/"):
        return pmap.get("fault_get") in permission
    if path.startswith("/api/v1/manu/elevator/alarm/statistics/"):
        return pmap.get("fault_get") in permission

    # 日志
    if path.startswith("/api/v1/user/refresh/"):
        return pmap.get("log_get") in permission
    if path.startswith("/api/v1/user/logsV2/"):
        return pmap.get("log_get") in permission

    if path.startswith("/api/v1/sta/checkup/elevator/"):
        return pmap.get("elevator_checkup") in permission

    # 创建配件时获取电梯和人员
    if path.startswith("/api/v1/basic/fittings/persons/"):
        return pmap.get("fittings_post") in permission or pmap.get("fittings_put") in permission

    # 配件下载
    if path.startswith("/api/v1/basic/fittings/download/"):
        return pmap.get("fittings_get") in permission

    # 配件增删改查
    if path.startswith("/api/v1/basic/fittings/"):
        return pmap.get("fittings_" + method) in permission

    # 故障会诊及分析获取人员和使用单位
    if path.startswith("api/v1/manu/elevator/fault/persons/"):
        return pmap.get("fault_analysis_get") in permission or pmap.get("fault_analysis_put") in permission or pmap.get("fault_analysis_delete") in permission

    # 故障会诊及分析下载
    if path.startswith("api/v1/manu/elevator/fault/download/"):
        return pmap.get("fault_analysis_get") in permission

    # 故障会诊及分析
    if path.startswith("api/v1/manu/elevator/fault/"):
        return pmap.get("fault_analysis_" + method) in permission

    if path.startswith("/api/v1/maintain/task/"):
        return
    if path.startswith("/api/v1/maintain/task/"):
        return
    if path.startswith("/api/v1/maintain/task/"):
        return
    if path.startswith("/api/v1/maintain/task/"):
        return
    if path.startswith("/api/v1/maintain/task/"):
        return
    if path.startswith("/api/v1/maintain/task/"):
        return
    if path.startswith("/api/v1/maintain/task/"):
        return
    if path.startswith("/api/v1/maintain/task/"):
        return
    if path.startswith("/api/v1/maintain/task/"):
        return
    if path.startswith("/api/v1/maintain/task/"):
        return
    if path.startswith("/api/v1/maintain/task/"):
        return
    if path.startswith("/api/v1/maintain/task/"):
        return
    if path.startswith("/api/v1/maintain/task/"):
        return
    if path.startswith("/api/v1/maintain/task/"):
        return






























